<template>
  <div class="app-container">
  <el-row :gutter="20">
    <!--部门数据-->
    <el-col :span="4" :xs="24">
      <div class="head-container">
        <el-input
            v-model="deptName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
        />
      </div>
      <div class="head-container">
        <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
        />
      </div>
    </el-col>
<!--    用户数据-->
  <el-col :span="20" :xs="24">
  <el-form :inline="true" :model="formInline" class="demo-form-inline">
    <el-form-item label="用户名">
      <el-input v-model="formInline.userName" clearable placeholder="请输入用户名称"></el-input>
    </el-form-item>
    <el-form-item label="手机号">
      <el-input v-model="formInline.phonenumber" clearable placeholder="请输入手机号"></el-input>
    </el-form-item>
    <el-form-item label="状态">
      <el-select v-model="formInline.status" clearable placeholder="用户状态">
        <el-option :value="0" label="正常">正常</el-option>
        <el-option :value="1" label="停用">停用</el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="创建时间">
      <el-date-picker
          v-model="formInline.createTimes"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" icon="el-icon-search" size="mini" @click="onSubmit">搜索</el-button>
      <el-button icon="el-icon-refresh" size="mini" @click="formclear">重置</el-button>
    </el-form-item>
  </el-form>
  <el-row :gutter="10" class="mb8">
    <el-col :span="1.5">
      <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="add"
      >新增</el-button>
    </el-col>
    <el-col :span="1.5">
      <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="update"
      >修改</el-button>
    </el-col>
    <el-col :span="1.5">
      <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="deletes"
      >删除</el-button>
    </el-col>
    <el-col :span="1.5">
      <el-upload
          class="upload-demo"
          action="http://localhost:8100/system/user/import"
          multiple
          :limit="3"
          accept=".xls,.xlsx"
      ><el-button size="small" type="primary">导入</el-button>
      </el-upload>
    </el-col>
    <el-col :span="1.5">
      <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="download"
      >导出</el-button>
    </el-col>
    <el-col :span="1.5">
      <el-button
          type="primary"
          size="mini"
          @click="initChart"
      >展示折现图</el-button>
    </el-col>

  </el-row>
  <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange">
    <el-table-column
        type="selection"
        width="55">
    </el-table-column>
    <el-table-column
        fixed
        prop="userId"
        label="用户编号"
        width="150">
    </el-table-column>
    <el-table-column
        prop="userName"
        label="用户名称"
        width="120">
    </el-table-column>
    <el-table-column
        prop="nickName"
        label="用户昵称"
        width="120">
    </el-table-column>
    <el-table-column
        prop="yphone"
        label="手机号"
        width="120">
    </el-table-column>
    <el-table-column
        label="状态"
        width="120">
      <template slot-scope="scope">
        <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
        ></el-switch>
      </template>
    </el-table-column>
    <el-table-column
        prop="createTime"
        label="创建时间"
        width="120">
    </el-table-column>
    <el-table-column
        label="头像"
        width="120">
      <template v-slot="scope">
        <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.avatar"></el-image>
      </template>
    </el-table-column>
    <el-table-column
        fixed="right"
        label="操作"
        width="100">
      <template slot-scope="scope">
        <el-button @click="del(scope.row.userId)" type="text" size="small">删除</el-button>
        <el-button @click="toupload(scope.row)" type="text" size="small">修改</el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-pagination
      @current-change="toPage"
      :current-page="page"
      :page-size="size"
      layout="total, prev, pager, next"
      :total="total">
  </el-pagination>
  </el-col>
  </el-row>
<!--    添加-->
  <el-dialog title="用户添加" :visible.sync="dialogFormVisible">
    <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户昵称" prop="nickName" :label-width="formLabelWidth">
        <el-input v-model="form.nickName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户名称" prop="userName" :label-width="formLabelWidth">
        <el-input v-model="form.userName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="头像" :label-width="formLabelWidth">
        <el-upload
            class="avatar-uploader"
            action="http://localhost:8100/system/user/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>

      </el-form-item>
      <el-form-item label="邮件" prop="email" :label-width="formLabelWidth">
        <el-input v-model="form.email" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户密码" prop="password" :label-width="formLabelWidth">
        <el-input v-model="form.passoword" show-password autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber" :label-width="formLabelWidth">
        <el-input v-model="form.phonenumber" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item label="性别" :label-width="formLabelWidth">
        <el-radio v-model="form.sex" label="0">男</el-radio>
        <el-radio v-model="form.sex" label="1">女</el-radio>
      </el-form-item>
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-radio v-model="form.status" label="0">正常</el-radio>
        <el-radio v-model="form.status" label="1">停用</el-radio>
      </el-form-item>
      <el-form-item label="归属部门" :label-width="formLabelWidth" prop="parentId">
        <el-select v-model="form.deptId" placeholder="请选择上级部门">
          <el-option   :label="deptName" :value="deptId"></el-option>
            <el-tree :data="deptData" :props="defaultProps" @node-click="handleNodeClick"></el-tree>

        </el-select>
      </el-form-item>
      <el-form-item label="岗位" :label-width="formLabelWidth">
        <el-select v-model="form.postId" multiple placeholder="请选择">
          <el-option
              v-for="item in postData"
              :key="item.postId"
              :label="item.postName"
              :value="item.postId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="角色" :label-width="formLabelWidth">
        <el-select v-model="form.roleId" multiple placeholder="请选择角色">
          <el-option
              v-for="item in roleData"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" :label-width="formLabelWidth">
        <el-input type="textarea" v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm('form')">添加</el-button>
      <el-button @click="resetForm('form')">取消</el-button>
    </div>
  </el-dialog>
<!--修改-->
  <el-dialog title="用户修改" :visible.sync="dialogFormVisible1">
    <el-form :model="user">
      <el-form-item label="用户名" :label-width="formLabelWidth">
        <el-input v-model="user.userName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户昵称" :label-width="formLabelWidth">
        <el-input v-model="user.nickName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="邮件" :label-width="formLabelWidth">
        <el-input v-model="user.email" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="手机号" :label-width="formLabelWidth">
        <el-input v-model="user.phonenumber" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="备注" :label-width="formLabelWidth">
        <el-input type="textarea" v-model="user.remark" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="归属部门" :label-width="formLabelWidth" prop="parentId">
        <el-select v-model="deptValue" placeholder="请选择上级部门">
          <el-option style="height: 100%; padding: 0%;" value="">
            <el-tree :data="deptData" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
          </el-option>

        </el-select>
      </el-form-item>
      <el-form-item label="岗位" :label-width="formLabelWidth">
        <el-select v-model="user.postId" multiple placeholder="请选择">
          <el-option
              v-for="item in postData"
              :key="item.postId"
              :label="item.postName"
              :value="item.postId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="角色" :label-width="formLabelWidth">
        <el-select v-model="user.roleId" multiple placeholder="请选择角色">
          <el-option
              v-for="item in roleData"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="性别" :label-width="formLabelWidth">
        <el-radio v-model="user.sex" label="0">男</el-radio>
        <el-radio v-model="user.sex" label="1">女</el-radio>
      </el-form-item>
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-radio v-model="user.status" label="0">正常</el-radio>
        <el-radio v-model="user.status" label="1">停用</el-radio>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible1 = false">取 消</el-button>
      <el-button type="primary" @click="update">确 定</el-button>
    </div>
  </el-dialog>
    <div>
    <div id="main" style="height: 300px;width: 500px"></div>
  </div>
</div>

</template>

<script>

export default {
  name: "UserView",
  data(){

    return{
      deptName:"",
      deptId:"",
      formInline:{},
      tableData:[],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      form:{},
      user:{},
      roleData:[],
      postData:[],
      deptData:[],
      zxt:[],
      ids:[],
      page:1,
      size:3,
      total:0,
      dialogFormVisible:false,
      dialogFormVisible1:false,
      formLabelWidth:'120px',
      multipleSelection:[],
      url:'',
      imageUrl:'',
      rules: {
        nickName: [
          { required: true, message: '请输入昵称', trigger: 'blur' },
        ],  phonenumber: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
        ], email: [
          { required: true, message: '请输入邮件', trigger: 'blur' },
        ],
        userName: [
          { required: true, message: '请输入名称', trigger: 'blur' },

        ],
        passoword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
      }
    }
  },methods:{
    //折线图
    initChart(){
      this.axios.post("system/user/gettu").then(r=>{
        this.zxt=r.data;
        let xDate=[];
        let yDate=[];
        this.zxt.forEach(item => {

          xDate.push(item.xzhou);
          yDate.push(item.yzhou);

          console.log(this.x);
          console.log(this.y);
        });
        this.x=xDate;
        this.y=yDate;
        var echarts = require('echarts');
        var chartDom = document.getElementById('main');
        var myChart = echarts.init(chartDom);
        var option;

        option = {
          xAxis: {
            type: 'category',
            data: this.x,
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              data: this.y,
              type: 'line'
            }
          ]
        };

        console.log(this.x)
        console.log(this.y);

        option && myChart.setOption(option);

      })
    },
    /**
     * 批量删除
     */
    deletes(){

      let ids1 = this.multipleSelection.map(obj=>obj.userId).join();
      console.log(ids1)
      this.axios.post("system/user/deletes?ids="+ids1).then(()=>{

          this.$message.success("删除成功");
          this.onSubmit();
      })
    },
    list1(){
      this.axios.post("/system/deptList",this.formInline).then(res=>{
        this.deptData = res.data.data;
      })
    },
    handleNodeClick(data) {

      this.deptId=data.id
      this.deptName=data.name

    },
    //岗位下拉
    postList(){
      this.axios.post("system/post/postList").then(r=>{
        this.postData=r.data.data;
      })
    },
    //角色下拉
    roleList(){
      this.axios.post("system/SysRole/roleList").then(r=>{
        this.roleData=r.data.data;
      })
    },
    //上传
    handleAvatarSuccess(res, file) {
     this.imageUrl=URL.createObjectURL(file.raw)
     this.form.avatar=res
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    //添加
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post("/system/user/add",this.form).then(r=>{
            if(r.data.code==200){
              this.$message.success("添加成功");
              this.dialogFormVisible=false;
              this.onSubmit();
              console.log(this.form);
            }else {
              this.$message.error(r.data.msg);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogFormVisible=false;
    },
    handleStatusChange(row){
      let text = row.status === "0" ? "启用" : "停用";
      this.$confirm('此操作将'+text+'该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })

    },
    //清除查询框
    formclear(){
  this.formInline={};
    },
    //下载excel
    download(){
     window.location.href=this.axios.defaults.baseURL+"system/user/download";
    },
    //修改
    update(){
  this.axios.post("system/user/upload",this.user).then(r=>{
    if(r.data.code==200){
      this.$message.success("修改成功");
      this.dialogFormVisible1=false;
      this.onSubmit();
    }else {
      this.$message.error(r.data.msg);
    }
  })
    },
    toupload(row){
      this.dialogFormVisible1=true;
      this.user=row;
      this.deptName=row.deptName;
      this.onSubmit();
    },
    del(userId){
      this.axios.post("system/user/delete?id="+userId,this.form).then(r=>{
        if(r.data.code==200){
          this.$message.success("删除成功");
          this.onSubmit();
        }else {
          this.$message.error(r.data.msg);
        }
      })
    },
    add(){
      this.dialogFormVisible=true
      this.form={status: "0",sex:"0",passoword: '123456',};
     this.imageUrl=""
      // this.form.status="0";
      // this.form.passoword="123456";

    },
    toPage(page){
      this.page=page;
      this.onSubmit();
    },
    onSubmit(){
      this.axios.post("/system/user/list?pageNum="+this.page+"&pageSize="+this.size,this.formInline).then(r=>{
        this.tableData=r.data.data.list;
        this.total=r.data.data.total;
      })
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      for (let i=0; i<val.length;i++){
        this.ids[i]=val[i].userId;
      }

    }
  },created() {
   this.onSubmit();
   this.roleList();
   this.postList();
   this.list1();
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>>